Vue.component('basetooltip', {
  template: `
    <div class="tool-tip-bk" v-if="showToolTip" @touchstart.self="showToolTip=false">
      <div class="tool-tip-wrap">
          <div class="arrow"></div>
          <div class="tool-tip">产品不会用？看看使用说明就懂了</div>
      </div>
    </div>`,
  data() {
    return {
      showToolTip: false,
    };
  },
  mounted() {
    this.judgePermissions();
  },
  methods: {
    // 判断是否展示提示(首次登陆和功能更新触发)
    judgeShowToolTip() {
      // 获取首次登录状态
      const isFirstLogin =
        window.localStorage.getItem(
          `introduceTip_${hxmConfig.limitUpConfig.sid}_${window.getUserid()}`
        ) || null;
      // 功能是否更新（需要每次更新在config.js中更新productUpadateDay字段）
      const isNew =
        window.localStorage.getItem(
          `productUpadate_${hxmConfig.limitUpConfig.sid}_${window.getUserid()}`
        ) || null;
      if (!isNew || isNew !== hxmConfig.limitUpConfig.productUpadateDay) {
        window.localStorage.setItem(
          `productUpadate_${hxmConfig.limitUpConfig.sid}_${window.getUserid()}`,
          hxmConfig.limitUpConfig.productUpadateDay
        );
        window.localStorage.setItem(
          `introduceTip_${hxmConfig.limitUpConfig.sid}_${window.getUserid()}`,
          1
        );
        this.showToolTip = true;
      } else if (!isFirstLogin || +isFirstLogin !== 1) {
        window.localStorage.setItem(
          `productUpadate_${hxmConfig.limitUpConfig.sid}_${window.getUserid()}`,
          hxmConfig.limitUpConfig.productUpadateDay
        );
        window.localStorage.setItem(
          `introduceTip_${hxmConfig.limitUpConfig.sid}_${window.getUserid()}`,
          1
        );
        this.showToolTip = true;
      }
    },

    // 2804跳转
    jumpBy2804(url) {
      if (!url || typeof url !== 'string') {
        return;
      }
      if (window.getAppVersion() && url.indexOf('client.html') === -1) {
        !url.startsWith('http') && (url = location.protocol + url);
        location.href = `client://client.html?action=ymtz^webid=2804^mode=new^url=${url}`;
      } else {
        location.href = url;
      }
    },

    // 设置右上角使用说明按钮
    setTitleRight() {
      window.callNativeHandler('notifyWebHandleEvent', {
        method: 'setTitleBar',
        params: {
          rightViewType: 'TextFunctionView',
          stopSetNil: 'yes',
          textContent: '使用说明',
          buttonName: 'submitBtn',
        },
      });
      window.callNativeHandler('changeWebViewButton');
      window.registerWebHandler('changeWebViewButton', data => {
        this.jumpBy2804(hxmConfig.limitUpConfig.introduce);
      });
    },

    // 判断是否有权限
    judgePermissions() {
      if (+window.getAccount() === 0) {
        return false;
      }
      $.ajax({
        type: 'get',
        url: hxmConfig.baseApi,
        dataType: 'json',
        data: {
          op: 'getLimitUpSummary',
        },
        cache: false,
        timeout: 10000,
      }).done(data => {
        if (+data.code === 0) {
          this.setTitleRight();
          this.judgeShowToolTip();
        }
      });
    },
  },
});
